home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.001 / tcpdump-~ / tcpdump-3.0.2-linux / libpcap-0.0.6 / grammar.y < prev    next >
Encoding:
Lex Description  |  1994-06-15  |  6.7 KB  |  265 lines

  1. %{
  2. /*
  3.  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
  4.  *    The Regents of the University of California.  All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that: (1) source code distributions
  8.  * retain the above copyright notice and this paragraph in its entirety, (2)
  9.  * distributions including binary code include the above copyright notice and
  10.  * this paragraph in its entirety in the documentation or other materials
  11.  * provided with the distribution, and (3) all advertising materials mentioning
  12.  * features or use of this software display the following acknowledgement:
  13.  * ``This product includes software developed by the University of California,
  14.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  15.  * the University nor the names of its contributors may be used to endorse
  16.  * or promote products derived from this software without specific prior
  17.  * written permission.
  18.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  19.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  20.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  *
  22.  */
  23. #ifndef lint
  24. static char rcsid[] =
  25.     "@(#) $Header: grammar.y,v 1.39 94/06/14 20:09:25 leres Exp $ (LBL)";
  26. #endif
  27.  
  28. #include <sys/types.h>
  29. #include <sys/time.h>
  30. #include <sys/socket.h>
  31.  
  32. #include <net/if.h>
  33. #include <net/bpf.h>
  34.  
  35. #include <netinet/in.h>
  36. #include <netinet/if_ether.h>
  37.  
  38. #include <stdio.h>
  39. #include <pcap.h>
  40. #include <pcap-namedb.h>
  41.  
  42. #include "gencode.h"
  43.  
  44. #define QSET(q, p, d, a) (q).proto = (p),\
  45.              (q).dir = (d),\
  46.              (q).addr = (a)
  47.  
  48. int n_errors = 0;
  49.  
  50. static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
  51.  
  52. static void
  53. yyerror(char *msg)
  54. {
  55.     ++n_errors;
  56.     bpf_error(msg);
  57.     /* NOTREACHED */
  58. }
  59.  
  60. #ifndef YYBISON
  61. pcap_parse()
  62. {
  63.     return (yyparse());
  64. }
  65. #endif
  66.  
  67. %}
  68.  
  69. %union {
  70.     int i;
  71.     u_long h;
  72.     u_char *e;
  73.     char *s;
  74.     struct stmt *stmt;
  75.     struct arth *a;
  76.     struct {
  77.         struct qual q;
  78.         struct block *b;
  79.     } blk;
  80.     struct block *rblk;
  81. }
  82.  
  83. %type    <blk>    expr id nid pid term rterm qid
  84. %type    <blk>    head
  85. %type    <i>    pqual dqual aqual ndaqual
  86. %type    <a>    arth narth
  87. %type    <i>    byteop pname pnum relop irelop
  88. %type    <blk>    and or paren not null prog
  89. %type    <rblk>    other
  90.  
  91. %token  DST SRC HOST GATEWAY
  92. %token  NET PORT LESS GREATER PROTO BYTE
  93. %token  ARP RARP IP TCP UDP ICMP
  94. %token  DECNET LAT MOPRC MOPDL
  95. %token  TK_BROADCAST TK_MULTICAST
  96. %token  NUM INBOUND OUTBOUND
  97. %token  LINK
  98. %token    GEQ LEQ NEQ
  99. %token    ID EID HID
  100. %token    LSH RSH
  101. %token  LEN
  102.  
  103. %type    <s> ID
  104. %type    <e> EID
  105. %type    <h> HID
  106. %type    <i> NUM
  107.  
  108. %left OR AND
  109. %nonassoc  '!'
  110. %left '|'
  111. %left '&'
  112. %left LSH RSH
  113. %left '+' '-'
  114. %left '*' '/'
  115. %nonassoc UMINUS
  116. %%
  117. prog:      null expr
  118. {
  119.     finish_parse($2.b);
  120. }
  121.     | null
  122.     ;
  123. null:      /* null */        { $$.q = qerr; }
  124.     ;
  125. expr:      term
  126.     | expr and term        { gen_and($1.b, $3.b); $$ = $3; }
  127.     | expr and id        { gen_and($1.b, $3.b); $$ = $3; }
  128.     | expr or term        { gen_or($1.b, $3.b); $$ = $3; }
  129.     | expr or id        { gen_or($1.b, $3.b); $$ = $3; }
  130.     ;
  131. and:      AND            { $$ = $<blk>0; }
  132.     ;
  133. or:      OR            { $$ = $<blk>0; }
  134.     ;
  135. id:      nid
  136.     | pnum            { $$.b = gen_ncode((u_long)$1,
  137.                            $$.q = $<blk>0.q); }
  138.     | paren pid ')'        { $$ = $2; }
  139.     ;
  140. nid:      ID            { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
  141.     | HID            {
  142.                   /* Decide how to parse HID based on proto */
  143.                   $$.q = $<blk>0.q;
  144.                   switch ($$.q.proto) {
  145.                   case Q_DECNET:
  146.                     $$.b =
  147.                         gen_ncode(__pcap_atodn((char *)$1),
  148.                         $$.q);
  149.                     break;
  150.                   default:
  151.                     $$.b =
  152.                         gen_ncode(__pcap_atoin((char *)$1),
  153.                         $$.q);
  154.                     break;
  155.                   }
  156.                 }
  157.     | EID            { $$.b = gen_ecode($1, $$.q = $<blk>0.q); }
  158.     | not id        { gen_not($2.b); $$ = $2; }
  159.     ;
  160. not:      '!'            { $$ = $<blk>0; }
  161.     ;
  162. paren:      '('            { $$ = $<blk>0; }
  163.     ;
  164. pid:      nid
  165.     | qid and id        { gen_and($1.b, $3.b); $$ = $3; }
  166.     | qid or id        { gen_or($1.b, $3.b); $$ = $3; }
  167.     ;
  168. qid:      pnum            { $$.b = gen_ncode((u_long)$1,
  169.                            $$.q = $<blk>0.q); }
  170.     | pid
  171.     ;
  172. term:      rterm
  173.     | not term        { gen_not($2.b); $$ = $2; }
  174.     ;
  175. head:      pqual dqual aqual    { QSET($$.q, $1, $2, $3); }
  176.     | pqual dqual        { QSET($$.q, $1, $2, Q_DEFAULT); }
  177.     | pqual aqual        { QSET($$.q, $1, Q_DEFAULT, $2); }
  178.     | pqual PROTO        { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
  179.     | pqual ndaqual        { QSET($$.q, $1, Q_DEFAULT, $2); }
  180.     ;
  181. rterm:      head id        { $$ = $2; }
  182.     | paren expr ')'    { $$.b = $2.b; $$.q = $1.q; }
  183.     | pname            { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
  184.     | arth relop arth    { $$.b = gen_relation($2, $1, $3, 0);
  185.                   $$.q = qerr; }
  186.     | arth irelop arth    { $$.b = gen_relation($2, $1, $3, 1);
  187.                   $$.q = qerr; }
  188.     | other            { $$.b = $1; $$.q = qerr; }
  189.     ;
  190. /* protocol level qualifiers */
  191. pqual:      pname
  192.     |            { $$ = Q_DEFAULT; }
  193.     ;
  194. /* 'direction' qualifiers */
  195. dqual:      SRC            { $$ = Q_SRC; }
  196.     | DST            { $$ = Q_DST; }
  197.     | SRC OR DST        { $$ = Q_OR; }
  198.     | DST OR SRC        { $$ = Q_OR; }
  199.     | SRC AND DST        { $$ = Q_AND; }
  200.     | DST AND SRC        { $$ = Q_AND; }
  201.     ;
  202. /* address type qualifiers */
  203. aqual:      HOST            { $$ = Q_HOST; }
  204.     | NET            { $$ = Q_NET; }
  205.     | PORT            { $$ = Q_PORT; }
  206.     ;
  207. /* non-directional address type qualifiers */
  208. ndaqual:  GATEWAY        { $$ = Q_GATEWAY; }
  209.     ;
  210. pname:      LINK            { $$ = Q_LINK; }
  211.     | IP            { $$ = Q_IP; }
  212.     | ARP            { $$ = Q_ARP; }
  213.     | RARP            { $$ = Q_RARP; }
  214.     | TCP            { $$ = Q_TCP; }
  215.     | UDP            { $$ = Q_UDP; }
  216.     | ICMP            { $$ = Q_ICMP; }
  217.     | DECNET        { $$ = Q_DECNET; }
  218.     | LAT            { $$ = Q_LAT; }
  219.     | MOPDL            { $$ = Q_MOPDL; }
  220.     | MOPRC            { $$ = Q_MOPRC; }
  221.     ;
  222. other:      pqual TK_BROADCAST    { $$ = gen_broadcast($1); }
  223.     | pqual TK_MULTICAST    { $$ = gen_multicast($1); }
  224.     | LESS NUM        { $$ = gen_less($2); }
  225.     | GREATER NUM        { $$ = gen_greater($2); }
  226.     | BYTE NUM byteop NUM    { $$ = gen_byteop($3, $2, $4); }
  227.     | INBOUND        { $$ = gen_inbound(0); }
  228.     | OUTBOUND        { $$ = gen_inbound(1); }
  229.     ;
  230. relop:      '>'            { $$ = BPF_JGT; }
  231.     | GEQ            { $$ = BPF_JGE; }
  232.     | '='            { $$ = BPF_JEQ; }
  233.     ;
  234. irelop:      LEQ            { $$ = BPF_JGT; }
  235.     | '<'            { $$ = BPF_JGE; }
  236.     | NEQ            { $$ = BPF_JEQ; }
  237.     ;
  238. arth:      pnum            { $$ = gen_loadi($1); }
  239.     | narth
  240.     ;
  241. narth:      pname '[' arth ']'        { $$ = gen_load($1, $3, 1); }
  242.     | pname '[' arth ':' NUM ']'    { $$ = gen_load($1, $3, $5); }
  243.     | arth '+' arth            { $$ = gen_arth(BPF_ADD, $1, $3); }
  244.     | arth '-' arth            { $$ = gen_arth(BPF_SUB, $1, $3); }
  245.     | arth '*' arth            { $$ = gen_arth(BPF_MUL, $1, $3); }
  246.     | arth '/' arth            { $$ = gen_arth(BPF_DIV, $1, $3); }
  247.     | arth '&' arth            { $$ = gen_arth(BPF_AND, $1, $3); }
  248.     | arth '|' arth            { $$ = gen_arth(BPF_OR, $1, $3); }
  249.     | arth LSH arth            { $$ = gen_arth(BPF_LSH, $1, $3); }
  250.     | arth RSH arth            { $$ = gen_arth(BPF_RSH, $1, $3); }
  251.     | '-' arth %prec UMINUS        { $$ = gen_neg($2); }
  252.     | paren narth ')'        { $$ = $2; }
  253.     | LEN                { $$ = gen_loadlen(); }
  254.     ;
  255. byteop:      '&'            { $$ = '&'; }
  256.     | '|'            { $$ = '|'; }
  257.     | '<'            { $$ = '<'; }
  258.     | '>'            { $$ = '>'; }
  259.     | '='            { $$ = '='; }
  260.     ;
  261. pnum:      NUM
  262.     | paren pnum ')'    { $$ = $2; }
  263.     ;
  264. %%
  265.